package com.learn.acm.design.observer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhongshitao
 * @date 2020/11/5 5:41 下午
 */
public class EventManager {

    public EventManager(String... operations) {
        for (String operation : operations) {
            this.listeners.put(operation, new ArrayList<>());
        }
    }

    Map<String, List<EventListeners>> listeners = new HashMap<>();


    public void subscribe(String envType, EventListeners eventListener) {
        List<EventListeners> eventListeners = listeners.get(envType);
        eventListeners.add(eventListener);
        listeners.put(envType, eventListeners);
    }

    public void unsubscribe(String envType, EventListeners eventListener) {
        List<EventListeners> eventListeners = listeners.get(envType);
        eventListeners.remove(eventListener);
        listeners.put(envType, eventListeners);
    }

    public void notify(String envType, String data) {
        for (EventListeners eventListener : listeners.get(envType)) {
            eventListener.update(data);
        }
    }
}
